home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / brushtop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  2.4 KB  |  107 lines

  1. /* brushtopbm.c - read a doodle brush file and write a portable bitmap
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. static void getinit ARGS(( FILE* file, int* colsP, int* rowsP ));
  16. static bit getbit ARGS(( FILE* file ));
  17.  
  18. void
  19. main( argc, argv )
  20.     int argc;
  21.     char* argv[];
  22.     {
  23.     FILE* ifp;
  24.     bit* bitrow;
  25.     register bit* bP;
  26.     int rows, cols, padright, row, col;
  27.  
  28.     pbm_init( &argc, argv );
  29.  
  30.     if ( argc > 2 )
  31.     pm_usage( "[brushfile]" );
  32.  
  33.     if ( argc == 2 )
  34.     ifp = pm_openr( argv[1] );
  35.     else
  36.     ifp = stdin;
  37.  
  38.     getinit( ifp, &cols, &rows );
  39.  
  40.     pbm_writepbminit( stdout, cols, rows, 0 );
  41.     bitrow = pbm_allocrow( cols );
  42.  
  43.     /* Compute padding to round cols up to the next multiple of 16. */
  44.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  45.  
  46.     for ( row = 0; row < rows; ++row )
  47.     {
  48.     /* Get data. */
  49.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  50.         *bP = getbit( ifp );
  51.     /* Discard line padding. */
  52.         for ( col = 0; col < padright; ++col )
  53.         (void) getbit( ifp );
  54.     /* Write row. */
  55.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  56.     }
  57.  
  58.     pm_close( ifp );
  59.     pm_close( stdout );
  60.     
  61.     exit( 0 );
  62.     }
  63.  
  64.  
  65. static int item, bitsperitem, bitshift;
  66.  
  67. static void
  68. getinit( file, colsP, rowsP )
  69.     FILE* file;
  70.     int* colsP;
  71.     int* rowsP;
  72.     {
  73.     int i;
  74.  
  75.     if ( getc( file ) != 1 )
  76.     pm_error( "bad magic number 1" );
  77.     if ( getc( file ) != 0 )
  78.     pm_error( "bad magic number 2" );
  79.     *colsP = getc( file ) << 8;
  80.     *colsP += getc( file );
  81.     *rowsP = getc( file ) << 8;
  82.     *rowsP += getc( file );
  83.     bitsperitem = 8;
  84.  
  85.     /* Junk rest of header. */
  86.     for ( i = 0; i < 10; ++i )  /* 10 is just a guess at the header size */
  87.     (void) getc( file );
  88.     }
  89.  
  90. static bit
  91. getbit( file )
  92.     FILE* file;
  93.     {
  94.     bit b;
  95.  
  96.     if ( bitsperitem == 8 )
  97.     {
  98.     item = getc( file );
  99.     bitsperitem = 0;
  100.     bitshift = 7;
  101.     }
  102.     ++bitsperitem;
  103.     b = ( ( item >> bitshift) & 1 ) ? PBM_WHITE : PBM_BLACK;
  104.     --bitshift;
  105.     return b;
  106.     }
  107.